<template>
  <div class="_wz_cent_main _wz_dataDictAdd">
    <Form ref="formAdd" :model="formData" label-position="right" :label-width="80" :rules="ruleValidate">
      <FormItem label="字典类型" prop="type">
        <i-select v-model="formData.type" filterable placeholder="请选择字典类型" clearable :label-in-value="true" @on-change="public.selectName($event,formData,'name')">
          <Option :value="item.code" :key="item.id" v-for="item in companyDict">{{ item.value }}</Option>
        </i-select>
      </FormItem>
      <FormItem label="字典数值" prop="value">
        <i-input v-model="formData.value" placeholder="请填写字典数值" clearable />
      </FormItem>
      <div class="demo-drawer-footer">
        <Button @click="$emit('closeFun', false)">取消</Button>
        <Button type="primary" @click="addSubmit('formAdd')">提交</Button>
      </div>
    </Form>
    <Spin size="large" fix v-if="spinShow"></Spin>

  </div>
</template>
<script>

  export default {
    props: ['selectData'],//接收来自父组件的数据
    data() {
      return {
        spinShow:false,//本页加载
        formData: {},//新增修改表单数据
        //验证
        ruleValidate: {
          type: [
            {required: true, message: '请选择字典类型', trigger: 'blur'},
          ],
          value: [
            {required: true, message: '请填写字典数值', trigger: 'blur'},
          ],
        },
        formTitle: "",//表单名称
        companyDict:[],//企业字典
      }
    },
    beforeCreate() {//beforeCreate创建前状态

    },
    created() {//created创建完毕状态
    },
    beforeMount() {//beforeMount挂载前状态   DOM 元素然后塞入页面的过程

    },
    mounted() {//mounted 挂载结束状态
      var that = this;
      //企业字典
      that.axios.post(that.apiUrl.systemDictSystemDictList, {type: "company_dict"}).then(res => {
        if (!!res) {
          that.companyDict = res.data.data;
        }
      }).catch(err => {
        console.log("失败", err)
      })

      that.formData = that.selectData;

    },
    methods: {//执行的方法
      addSubmit(name) {//新增修改提交
        let that = this;
        that.$refs[name].validate((valid) => {
          if (valid) {//正则判断使用
            that.spinShow = true;//显示加载
            that.axios.post(that.apiUrl.coreDeptDictCoreDeptDictSaveOrUpdate, that.formData).then(res => {
              that.spinShow = false;//显示加载
              if (!!res) {
                that.$emit("closeFun", true)
                that.$Message.success('保存成功');
              }
            }).catch(err => {
              console.log("失败", err)
            })

          } else {
            this.$Message.error('请正确填写表单');
          }
        })
      },
    },
    watch: {//监听

    },
    beforeUpdate() {//beforeUpdate 更新前状态

    },
    updated() {//updated 更新完成状态

    },
    beforeDestroy() {//beforeDestroy 销毁前状态

    },
    destroyed() {//destroyed 销毁完成状态

    }
  }
</script>
<style lang="less">
  ._wz_dataDictAdd {
    display: flex;
    flex-direction: column;
  }
</style>
